package com.bush.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bush.mall.product.entity.dto.BrandDTO;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import com.bush.mall.common.entity.Result;
import com.bush.mall.common.exception.SaaSException;
import com.bush.mall.product.mapper.BrandMapper;
import com.bush.mall.product.entity.BrandEntity;
import com.bush.mall.product.service.BrandService;
import org.springframework.util.StringUtils;

/**
 * 品牌
 * @author RUN
 * @date 2022-11-28
 */
@Service(BrandService.BEAN_ID)
@Slf4j
public class BrandServiceImpl extends ServiceImpl<BrandMapper, BrandEntity> implements BrandService {

    /**
     * 查询品牌列表
     * @author RUN
     * @date 2022-11-28
     */
    @Override
    public Result queryForPage(BrandDTO dto) {
        try{
            Assert.notNull(dto,"参数异常");
            Page<BrandEntity> page = new Page<>(dto.getCurrent(),dto.getSize());
            LambdaQueryWrapper<BrandEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.orderByDesc(BrandEntity::getSort);
            if (StringUtils.hasText(dto.getName())) wrapper.like(BrandEntity::getName,dto.getName());
            if (StringUtils.hasText(dto.getFirstLetter())) wrapper.like(BrandEntity::getFirstLetter,dto.getFirstLetter());
            if (Objects.nonNull(dto.getShowStatus())) wrapper.eq(BrandEntity::getShowStatus,dto.getShowStatus());
            baseMapper.selectPage(page,wrapper);
            return Result.ok(page);
        }catch(Exception e){
            log.error("BrandService queryForPage ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 创建品牌表单对象
     * @author RUN
     * @date 2022-11-28
     */
    @Override
    public Result saveBrand(BrandEntity brand) {
        try{
            Assert.notNull(brand,"参数异常");
            brand.setShowStatus(1);
            boolean flag = this.save(brand);
            if (flag) return Result.ok("品牌添加成功");
            throw new SaaSException("品牌添加失败");
        }catch(Exception e){
            log.error("BrandService saveBrand ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改品牌表单对象
     * @author RUN
     * @date 2022-11-28
     */
    @Override
    public Result updateBrand(BrandEntity brand) {
        try{
            Assert.notNull(brand,"参数异常");
            boolean flag = this.updateById(brand);
            if (flag) return Result.ok("品牌修改成功");
            throw new SaaSException("品牌修改失败");
        }catch(Exception e){
            log.error("BrandService updateBrand ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 通过id删除品牌表单
     * @author RUN
     * @date 2022-11-28
     */
    @Override
    public Result deleteBrand(Long id) {
        try{
            Assert.notNull(id,"参数异常");
            boolean flag = this.removeById(id);
            if (flag) return Result.ok("品牌删除成功");
            throw new SaaSException("品牌删除失败");
        }catch(Exception e){
            log.error("BrandService deleteBrand ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 更改是否显示品牌状态
     * @param id id
     * @return {@link Result}
     */
    @Override
    public Result changeStstua(Long id) {
        try{
            Assert.notNull(id,"参数异常");
            BrandEntity brand = this.getById(id);
            Integer showStatus = brand.getShowStatus();
            int status = showStatus == 0 ? 1 : 0;
            brand.setShowStatus(status);
            boolean flag = this.updateById(brand);
            if (flag) return Result.ok().success(status == 0 ? "品牌取消显示成功!" : "品牌打开显示成功!");
            throw new SaaSException("更改品牌状态失败!");
        }catch(Exception e){
            log.error("BrandService changeStstua ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 批量删除品牌
     * @param ids id
     * @return {@link Result}
     */
    @Override
    public Result deleteBatchBrand(List<Long> ids) {
        try{
            Assert.notNull(ids,"参数异常");
            boolean flag = this.removeByIds(ids);
            if (flag) return Result.ok().success("批量删除品牌成功！");
            throw new SaaSException("批量删除品牌失败！");
        }catch(Exception e){
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }


}